/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.hanfu.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.hanfu.core.oss.OSS;
import top.continew.admin.hanfu.domain.Vneue;
import top.continew.admin.hanfu.domain.bo.VneueBo;
import top.continew.admin.hanfu.domain.vo.VneueVo;
import top.continew.admin.hanfu.domain.vo.wx.VneueWxVo;
import top.continew.admin.hanfu.mapper.VneueMapper;
import top.continew.admin.hanfu.service.IVneueService;

import java.util.Arrays;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author bbm
 * @since 2025-04-06
 */
@Service
@RequiredArgsConstructor
public class VneueServiceImpl extends ServiceImpl<VneueMapper, Vneue> implements IVneueService {

    private final VneueMapper vneueMapper;

    private final OSS oss;

    @Override
    public Page<VneueVo> InquiryVneue(Page<VneueVo> page, VneueBo vneueBo) {

        Page<VneueVo> vneueVoPage = vneueMapper.InquiryVneue(page, vneueBo);

        vneueVoPage.getRecords().forEach(i -> {
            i.setUpdateTime(DateTime.of(Long.parseLong(i.getUpdateTime())).toString("yyyy-MM-dd HH:mm:ss"));
        });

        return vneueVoPage;
    }

    @Override
    public Integer deleteVneue(Integer id) {

        Vneue byId = getById(id);

        String ossId = byId.getOssId();

        if (ossId != null) {
            Arrays.stream(ossId.split(",")).toList().forEach(i -> {
                oss.deleteFile(Integer.parseInt(i));
            });
        }

        boolean b = removeById(byId);

        return b ? 1 : 0;
    }

    @Override
    public Page<VneueWxVo> InquiryVneueWx(Page<VneueWxVo> page, VneueBo vneueBo) {
        return vneueMapper.InquiryVneueWx(page, vneueBo);
    }

}
